package tpase;

import tpase.KeyServer.Controller.KeyServerController;
import tpase.KeyServer.VO.SetupParam;
import tpase.util.Util;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static int t, n;
    public static boolean isSetup = false;
    public static boolean isDebug = true;
    public static void main(String[] args) throws InterruptedException {
        keyServerStart();
    }

    public static void keyServerStart() throws InterruptedException {
        //读取公共参数
        String setupPath = "src/main/resources/config/setup/parameters.json";
        SetupParam setupParam = Util.jsonFileToObj(setupPath, SetupParam.class);
        t = setupParam.getT();
        n = setupParam.getN();

        if (isDebug) {
            System.out.printf("总服务器数量：%s，门限服务器数量：%s \n", n, t);
        }


        String serverPath = "src/main/resources/config/server";
        List<KeyServerController> keyServerControllerList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String paramPath = serverPath + i;
            KeyServerController keyServerController = new KeyServerController(paramPath, String.valueOf(i), isDebug);
            if (isSetup == false) {
                keyServerController.setup();
            }
            keyServerControllerList.add(keyServerController);
        }


        System.out.println("KeyServer setup");
        //多线程并行计算
        for (int i = 0; i < n; i++) {
            KeyServerController keyServerController = keyServerControllerList.get(i);
            keyServerController.start();
        }




    }
}